import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import debug from 'debug';

/**
 * 日志级别
 */
export var LoggerLevel = /*#__PURE__*/function (LoggerLevel) {
  LoggerLevel[LoggerLevel["SUCCESS"] = 5] = "SUCCESS";
  LoggerLevel[LoggerLevel["LOG"] = 4] = "LOG";
  LoggerLevel[LoggerLevel["DEBUG"] = 3] = "DEBUG";
  LoggerLevel[LoggerLevel["WARN"] = 2] = "WARN";
  LoggerLevel[LoggerLevel["ERROR"] = 1] = "ERROR";
  return LoggerLevel;
}({});
/**
 * 日志打印
 */
export var Logger = /*#__PURE__*/function () {
  function Logger(namespace, driver) {
    _classCallCheck(this, Logger);
    /** 特定前缀，作为筛分的命名空间 */
    _defineProperty(this, "namespace", '');
    _defineProperty(this, "driver", void 0);
    this.namespace = namespace;
    this.driver = {
      success: debug(namespace),
      log: debug(namespace),
      debug: debug(namespace),
      warn: debug(namespace),
      error: debug(namespace)
    };
    if (driver) this.setDriver(driver);
  }
  _createClass(Logger, [{
    key: "setDriver",
    value: function setDriver(driver) {
      this.driver = _objectSpread(_objectSpread({}, this.driver), driver);
    }
  }, {
    key: "success",
    value: function success(msg) {
      if (Logger.level < LoggerLevel.SUCCESS) return;
      if (this.driver.success === console.log) {
        console.log("%c ".concat(this.namespace), 'color: #52c41a; font-weight: bold', msg);
      } else {
        var _this$driver$success, _this$driver;
        (_this$driver$success = (_this$driver = this.driver).success) === null || _this$driver$success === void 0 || _this$driver$success.call(_this$driver, msg);
      }
    }
  }, {
    key: "log",
    value: function log(msg) {
      if (Logger.level < LoggerLevel.LOG) return;
      if (this.driver.log === console.log) {
        console.log("%c ".concat(this.namespace), 'color: #1677ff; font-weight: bold', msg);
      } else {
        var _this$driver$log, _this$driver2;
        (_this$driver$log = (_this$driver2 = this.driver).log) === null || _this$driver$log === void 0 || _this$driver$log.call(_this$driver2, msg);
      }
    }
  }, {
    key: "debug",
    value: function debug(msg) {
      if (Logger.level < LoggerLevel.DEBUG) return;
      if (this.driver.debug === console.debug) {
        console.debug("%c ".concat(this.namespace), 'color: #faad14; font-weight: bold', msg);
      } else {
        var _this$driver$debug, _this$driver3;
        (_this$driver$debug = (_this$driver3 = this.driver).debug) === null || _this$driver$debug === void 0 || _this$driver$debug.call(_this$driver3, msg);
      }
    }
  }, {
    key: "warn",
    value: function warn(msg) {
      if (Logger.level < LoggerLevel.WARN) return;
      if (this.driver.warn === console.warn) {
        console.warn("%c ".concat(this.namespace), 'color: #faad14; font-weight: bold', msg);
      } else {
        var _this$driver$warn, _this$driver4;
        (_this$driver$warn = (_this$driver4 = this.driver).warn) === null || _this$driver$warn === void 0 || _this$driver$warn.call(_this$driver4, msg);
      }
    }
  }, {
    key: "error",
    value: function error(msg) {
      if (Logger.level < LoggerLevel.ERROR) return;
      if (this.driver.error === console.error) {
        console.error("%c ".concat(this.namespace), 'color: #f5222d; font-weight: bold', msg);
      } else {
        var _this$driver$error, _this$driver5;
        (_this$driver$error = (_this$driver5 = this.driver).error) === null || _this$driver$error === void 0 || _this$driver$error.call(_this$driver5, msg);
      }
    }
  }], [{
    key: "setLevel",
    value: function setLevel(level) {
      Logger.level = level;
    }
  }]);
  return Logger;
}();

/**
 * 全局仅有一个实例
 */
_defineProperty(Logger, "level", LoggerLevel.LOG);
export var logger = new Logger('szzj');